﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace game2048
{
    public partial class Form1 : Form
    {
        //可以调整方格大小
        int label_width=50;
        //方便操作Label建立的数组
        static Label[,] g_Label_Array;
        //分数的Label
        static Label g_Label_Score;
        //说明的Label
        static Label g_Label_Comment;
        //实例化
        Game2048 one = null;
        public Form1()
        {
            InitializeComponent();
        }
        private void GenUI()
        {
            //由设定生成Label，并放入数组管理
            g_Label_Array = new Label[Game2048.g_line, Game2048.g_line];
            for(int i=0;i<Game2048.g_line;i++)
            {
                for(int j=0;j<Game2048.g_line;j++)
                {
                    g_Label_Array[i, j] = new Label();
                    g_Label_Array[i, j].Parent = this;
                    g_Label_Array[i, j].Text = "";
                    g_Label_Array[i, j].AutoSize = false;
                    g_Label_Array[i, j].BorderStyle = BorderStyle.FixedSingle;
                    g_Label_Array[i, j].TextAlign = ContentAlignment.MiddleCenter;
                    g_Label_Array[i, j].Font = new Font("Arial", 12);
                    g_Label_Array[i, j].Width = label_width;
                    g_Label_Array[i, j].Height = label_width;
                    g_Label_Array[i, j].Top = i * label_width;
                    g_Label_Array[i, j].Left = j * label_width;
                    g_Label_Array[i, j].Show();
                }
            }

            //分数绘制
            g_Label_Score = new Label();
            g_Label_Score.Parent = this;
            g_Label_Score.Text = Game2048.g_score.ToString();
            g_Label_Score.AutoSize = false;
            g_Label_Score.BorderStyle = BorderStyle.FixedSingle;
            g_Label_Score.TextAlign = ContentAlignment.MiddleCenter;
            g_Label_Score.Font = new Font("Arial", 12);
            g_Label_Score.Width = label_width * Game2048.g_line;
            g_Label_Score.Height = label_width;
            g_Label_Score.Top = label_width * Game2048.g_line;
            g_Label_Score.Left = 0;
            g_Label_Score.BackColor = Color.Aqua;
            g_Label_Score.Show();
            //说明绘制
            g_Label_Comment = new Label();
            g_Label_Comment.Parent = this;
            g_Label_Comment.Text = "上下左右：移动\nr：重新开始\nq：退出";
            g_Label_Comment.AutoSize = false;
            g_Label_Comment.BorderStyle = BorderStyle.FixedSingle;
            g_Label_Comment.TextAlign = ContentAlignment.MiddleCenter;
            g_Label_Comment.Font = new Font("Arial", 12);
            g_Label_Comment.Width = label_width * Game2048.g_line;
            g_Label_Comment.Height = label_width*2;
            g_Label_Comment.Top = label_width * (Game2048.g_line+1);
            g_Label_Comment.Left = 0;
            g_Label_Comment.BackColor = Color.Aquamarine;
            g_Label_Comment.Show();
            //窗体设定
            this.FormBorderStyle = FormBorderStyle.None;
            this.Width = Game2048.g_line * label_width;
            this.Height = (Game2048.g_line + 3) * label_width;
            this.TopMost = true;
            this.StartPosition = FormStartPosition.CenterScreen;

        }
        private void RefreshUI()
        {
            for(int i=0;i< Game2048.g_line; i++)
            {
                for (int j = 0; j < Game2048.g_line; j++)
                {
                    //刷新文本
                    g_Label_Array[i, j].Text = Game2048.g_game_array[i, j].ToString();
                    //不同数字不同的文本
                    switch(Game2048.g_game_array[i,j])
                    {
                        case 2:
                            g_Label_Array[i, j].BackColor = Color.PaleTurquoise;
                            break;
                        case 4:
                            g_Label_Array[i, j].BackColor = Color.Thistle;
                            break;
                        case 8:
                            g_Label_Array[i, j].BackColor = Color.LightGreen;
                            break;
                        case 16:
                            g_Label_Array[i, j].BackColor = Color.LightSalmon;
                            break;
                        case 32:
                            g_Label_Array[i, j].BackColor = Color.Plum;
                            break;
                        case 64:
                            g_Label_Array[i, j].BackColor = Color.Tomato;
                            break;
                        case 128:
                            g_Label_Array[i, j].BackColor = Color.HotPink;
                            break;
                        case 256:
                            g_Label_Array[i, j].BackColor = Color.LightCoral;
                            break;
                        case 512:
                            g_Label_Array[i, j].BackColor = Color.DarkOrchid;
                            break;
                        case 1024:
                            g_Label_Array[i, j].BackColor = Color.Aqua;
                            break;
                        case 2048:
                            g_Label_Array[i, j].BackColor = Color.Crimson;
                            break;
                        default:
                            g_Label_Array[i, j].BackColor = Color.Moccasin;
                            break;

                    }
                }
            }
            //显示分数
            g_Label_Score.Text = Game2048.g_score.ToString();
            if (Game2048.g_lose)
                g_Label_Score.Text += "你输了";
            else if(Game2048.g_win)
                g_Label_Score.Text += "你赢了";

        }


        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 37)   //按下左键
            {
                if (!(Game2048.g_lose || Game2048.g_win))
                {
                    Game2048.Left();
                    RefreshUI();
                }
                
            }
            else if (e.KeyValue == 38)  //按下上键
            {
                if (!(Game2048.g_lose||Game2048.g_win))
                {
                    Game2048.Up();
                    RefreshUI();
                }
                
            }
            else if (e.KeyValue == 39)  //按下右键
            {
                if (!(Game2048.g_lose || Game2048.g_win))
                {
                    Game2048.Right();
                    RefreshUI();
                }
                
            }
            else if (e.KeyValue == 40)  //按下下键
            {
                if (!(Game2048.g_lose || Game2048.g_win))
                {
                    Game2048.Down();
                    RefreshUI();
                }
                
            }
            else if(e.KeyValue==81) //q键按下
            {
                //程序退出
                this.Close();
            }
            else if(e.KeyValue==82) //r键按下
            {
                //重启游戏
                Game2048.Restart();
                RefreshUI();

            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                //选择棋盘矩阵大小
                InputBox ib = new InputBox();
                if(ib.ShowDialog()==DialogResult.OK)
                {
                    //实例化
                    one = Game2048.GetInstance();
                    //设定矩阵大小
                    Game2048.g_line = ib.Result;
                    //产生界面
                    GenUI();
                    //开始游戏
                    Game2048.Restart();
                    //刷新界面
                    RefreshUI();
                }
                
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}
